import { axiosAccountIns } from '../axios';

const URL: Record<keyof IService.Account.HTTP, string> = {
    login: '/auth/doLogin',
    logout: '/auth/doLogout',
    getLoginUser: '/auth/getLoginUser',
    getUserList: '/user/page',
    createUser: '/user/add',
    deleteUser: '/user/delete',
    resetPassword: '/user/setPassword',
    assignRoles: '/user/grantRole',
    getRoleList: '/role/page',
    createRole: '/role/add',
    updateRole: '/role/edit',
    deleteRole: '/role/delete',
    getPermissionList:'/permissions/page'
};

export async function login(request: IService.Account.HTTP['login']['request']): Promise<IService.Account.HTTP['login']['response']> {
    request.password = MyModule.smCrypto.sm2(request.password);
    return axiosAccountIns.post(URL.login, request);
}

export async function logout(request: IService.Account.HTTP['logout']['request']): Promise<IService.Account.HTTP['logout']['response']> {
    return axiosAccountIns.get(URL.logout, { params: request });
}

export async function getLoginUser(request: IService.Account.HTTP['getLoginUser']['request']): Promise<IService.Account.HTTP['getLoginUser']['response']> {
    return axiosAccountIns.get(URL.getLoginUser, { params: request });
}

export async function getUserList(request: IService.Account.HTTP['getUserList']['request']): Promise<IService.Account.HTTP['getUserList']['response']> {
    return axiosAccountIns.post(URL.getUserList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}

export async function createUser(request: IService.Account.HTTP['createUser']['request']): Promise<IService.Account.HTTP['createUser']['response']> {
    request.password = MyModule.smCrypto.sm2(request.password);
    return axiosAccountIns.post(URL.createUser, request);
}

export async function deleteUser(request: IService.Account.HTTP['deleteUser']['request']): Promise<IService.Account.HTTP['deleteUser']['response']> {
    return axiosAccountIns.post(URL.deleteUser, request);
}

export async function resetPassword(request: IService.Account.HTTP['resetPassword']['request']): Promise<IService.Account.HTTP['resetPassword']['response']> {
    request.password = MyModule.smCrypto.sm2(request.password);
    return axiosAccountIns.post(URL.resetPassword, request);
}

export async function assignRoles(request: IService.Account.HTTP['assignRoles']['request']): Promise<IService.Account.HTTP['assignRoles']['response']> {
    return axiosAccountIns.post(URL.assignRoles, request);
}

export async function getRoleList(request: IService.Account.HTTP['getRoleList']['request']): Promise<IService.Account.HTTP['getRoleList']['response']> {
    return axiosAccountIns.post(URL.getRoleList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}

export async function createRole(request: IService.Account.HTTP['createRole']['request']): Promise<IService.Account.HTTP['createRole']['response']> {
    return axiosAccountIns.post(URL.createRole, request);
}

export async function updateRole(request: IService.Account.HTTP['updateRole']['request']): Promise<IService.Account.HTTP['updateRole']['response']> {
    return axiosAccountIns.post(URL.updateRole, request);
}

export async function deleteRole(request: IService.Account.HTTP['deleteRole']['request']): Promise<IService.Account.HTTP['deleteRole']['response']> {
    return axiosAccountIns.post(URL.deleteRole, request);
}

export async function getPermissionList(request: IService.Account.HTTP['getPermissionList']['request']): Promise<IService.Account.HTTP['getPermissionList']['response']> {
    return axiosAccountIns.post(URL.getPermissionList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}
